package com.yoyo.controller;

import com.yoyo.entity.Pet;
import com.yoyo.entity.PetType;
import com.yoyo.entity.ResultBulls;
import com.yoyo.service.PetService;
import com.yoyo.service.PetTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(path = "/feeding")
public class PetFeedingController {

    @Autowired
    private PetTypeService petTypeService;
    @Autowired
    private PetService petService;

    /**
     * 加载petType
     * @return
     */
    @GetMapping("petType")
    public String feeding(){
        return "Petfeeding";
    }


    /**
     * 查询petType表全部
     * @param model
     * @param petType
     * @return
     */
    @RequestMapping(path = "/petTypeAll")
    @ResponseBody
    public List<PetType> findAll(Model model, PetType petType){
        List<PetType> petTypeList  =petTypeService.findAll(petType);
        model.addAttribute("PetTypeAll",petTypeList);
        return petTypeList;
    }
    /**
     *加载pet
     * @return
     */
    @GetMapping("pet")
    public String petAll(){
        return "Petfeeding";
    }

    /**
     * 查询pet表全部
     * @param model
     * @param pet
     * @return
     */
    @RequestMapping(path = "/petAll")
    @ResponseBody
    public List<Pet> findAll(Model model, Pet pet){
        List<Pet> petList  =petService.findAll(pet);
        model.addAttribute("PetAll",petList);
        System.out.println("+++++++++++++++++Controller"+petList);
        return petList;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(path = "delete")
    @ResponseBody
    public ResultBulls delete(Integer id){
        int i = petService.delete(id);
        ResultBulls bs = new ResultBulls();
        if(i>0){
            bs.setCode(200);
        }else{
            bs.setCode(500);
        }
        return bs;
    }


    @ResponseBody
    @RequestMapping("add")
    public ResultBulls add(Pet pet){
        ResultBulls bs = new ResultBulls();
        try{
            int i = petService.save(pet);
            bs.setCode(200);
        }catch (Exception e){
            bs.setCode(500);
            e.printStackTrace();
        }

        return bs;
    }



    @ResponseBody
    @RequestMapping("batchDel")
    public ResultBulls batchDel(String ids){
        ResultBulls bs = new ResultBulls();
        try{
            int i = petService.bacthDel(ids);
            bs.setCode(200);
        }catch (Exception e){
            bs.setCode(500);
            e.printStackTrace();
        }

        return bs;
    }

    @ResponseBody
    @RequestMapping("selById")
    public ResultBulls batchDel(int id){
        ResultBulls bs = new ResultBulls();
        try{
            Pet pet  = petService.selById(id);
            bs.setCode(200);
            bs.setData(pet);
        }catch (Exception e){
            bs.setCode(500);
            e.printStackTrace();
        }
        return bs;
    }

    @ResponseBody
    @RequestMapping("upd")
    public ResultBulls upd(Pet pet){
        ResultBulls bs = new ResultBulls();
        try{
            int index = petService.updPet(pet);
            bs.setCode(200);
            bs.setData(pet);
        }catch (Exception e){
            bs.setCode(500);
            e.printStackTrace();
        }
        return bs;
    }






}
